package com.azlogica.entity;

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.os.BatteryManager;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;

public class Frame {

	Location location;
	public String frame;
	Context c;
	TelephonyManager mngr;
	GsmCellLocation cl;
	private final static int HEADER_VALUE = 5;
	private final static int ITEM_MASK_VALUE = 31;
	private final static int NUMBER_VALUE = 1;
	private final static int SACK_VALUE = 0;
	// ---------Frame attributes-----------//
	private int id;
	private int header;
	private int reportItemMask;
	private int sack;
	private int messType;
	private String imei;
	private int status;
	private int number;
	private int accuracy;
	private int speed;
	private int azimuth;
	private long altitude;
	private long longitude;
	private long latitude;
	private String gpsUTC;
	private int mcc;
	private int mnc;
	private int lac;
	private int cllId;
	private int battery;
	private String sendTime;
	private int serial;
	private int send;

	public Frame(Location location, Context context, int messType) {
		this.location = location;
		this.c = context;
		this.mngr = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		this.cl = (GsmCellLocation) mngr.getCellLocation();
		IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		Intent batteryStatus = c.registerReceiver(null, ifilter);
		int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
		SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		Date date = new Date(location.getTime());
		Calendar cal = Calendar.getInstance();
		String networkOperator = mngr.getNetworkOperator();
		DecimalFormat numberFormat = new DecimalFormat("#.000000");
		DecimalFormat numberFormat1 = new DecimalFormat("#.0");

		// ------------------------Some parameters
		// before-----------------------//

		this.header = HEADER_VALUE;
		this.reportItemMask = ITEM_MASK_VALUE;
		this.number = NUMBER_VALUE;
		this.sack = SACK_VALUE;
		this.messType = messType;
		this.serial = 0;
		this.status = 66; // Quemado
		this.mcc = Integer.parseInt(networkOperator.substring(0, 3));
		this.mnc = Integer.parseInt(networkOperator.substring(3));
		String formattedUTC = format.format(date);
		String formattedActualTime = format.format(cal.getTime());

		// ---------------------------------------------------------------------//
		this.imei = mngr.getDeviceId();
		this.accuracy = (int) location.getAccuracy();
		this.speed = Math.round(location.getSpeed());
		this.azimuth = Math.round(location.getBearing());

		if (String.valueOf(
				numberFormat1.format((double) location.getAltitude()))
				.contains(".")) {
			this.altitude = Integer.parseInt(String.valueOf(
					numberFormat1.format((double) location.getAltitude()))
					.replace(".", ""));
		} else if (String.valueOf(
				numberFormat1.format((double) location.getAltitude()))
				.contains(",")) {
			this.altitude = Integer.parseInt(String.valueOf(
					numberFormat1.format((double) location.getAltitude()))
					.replace(",", ""));
		}

		if (String.valueOf(
				numberFormat.format((double) location.getLongitude()))
				.contains(".")) {
			this.longitude = Integer.parseInt(String.valueOf(
					numberFormat.format((double) location.getLongitude()))
					.replace(".", ""));
		} else if (String.valueOf(
				numberFormat.format((double) location.getLongitude()))
				.contains(",")) {
			this.longitude = Integer.parseInt(String.valueOf(
					numberFormat.format((double) location.getLongitude()))
					.replace(",", ""));
		}

		if (String
				.valueOf(numberFormat.format((double) location.getLatitude()))
				.contains(".")) {
			this.latitude = Integer.parseInt(String.valueOf(
					numberFormat.format((double) location.getLatitude()))
					.replace(".", ""));
		} else if (String.valueOf(
				numberFormat.format((double) location.getLatitude())).contains(
				",")) {
			this.latitude = Integer.parseInt(String.valueOf(
					numberFormat.format((double) location.getLatitude()))
					.replace(",", ""));
		}
		this.gpsUTC = formattedUTC;
		this.lac = cl.getLac();
		this.cllId = cl.getCid();
		this.battery = level;
		this.sendTime = formattedActualTime;
		this.send=0;
	}

	public Frame(int id, int header, int reportItemMask, int sack,
			int messType, String imei, int status, int number, int accuracy,
			int speed, int azimuth, long altitude, long longitude,
			long latitude, String gpsUTC, int mcc, int mnc, int lac, int cllId,
			int battery, String sendTime, int serial, int send) {
		this.id = id;
		this.header = header;
		this.reportItemMask = reportItemMask;
		this.sack = sack;
		this.messType = messType;
		this.imei = imei;
		this.status = status;
		this.number = number;
		this.accuracy = accuracy;
		this.speed = speed;
		this.azimuth = azimuth;
		this.altitude = altitude;
		this.longitude = longitude;
		this.latitude = latitude;
		this.gpsUTC = gpsUTC;
		this.mcc = mcc;
		this.mnc = mnc;
		this.lac = lac;
		this.cllId = cllId;
		this.battery = battery;
		this.sendTime = sendTime;
		this.serial = serial;
		this.send = send;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public int getHeader() {
		return header;
	}

	public void setHeader(int header) {
		this.header = header;
	}

	public int getReport() {
		return reportItemMask;
	}

	public void setReport(int report) {
		this.reportItemMask = report;
	}

	public int getSack() {
		return sack;
	}

	public void setSack(int sack) {
		this.sack = sack;
	}

	public int getMessType() {
		return messType;
	}

	public void setMessType(int messType) {
		this.messType = messType;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public int getAccuracy() {
		return accuracy;
	}

	public void setAccuracy(int accuracy) {
		this.accuracy = accuracy;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public int getAzimuth() {
		return azimuth;
	}

	public void setAzimuth(int azimuth) {
		this.azimuth = azimuth;
	}

	public long getAltitude() {
		return altitude;
	}

	public void setAltitude(int altitude) {
		this.altitude = altitude;
	}

	public long getLongitude() {
		return longitude;
	}

	public void setLongitude(int longitude) {
		this.longitude = longitude;
	}

	public long getLatitude() {
		return latitude;
	}

	public void setLatitude(int latitude) {
		this.latitude = latitude;
	}

	public String getGpsUTC() {
		return gpsUTC;
	}

	public void setGpsUTC(String gpsUTC) {
		this.gpsUTC = gpsUTC;
	}

	public int getMcc() {
		return mcc;
	}

	public void setMcc(int mcc) {
		this.mcc = mcc;
	}

	public int getMnc() {
		return mnc;
	}

	public void setMnc(int mnc) {
		this.mnc = mnc;
	}

	public int getLac() {
		return lac;
	}

	public void setLac(int lac) {
		this.lac = lac;
	}

	public int getCllId() {
		return cllId;
	}

	public void setCllId(int cllId) {
		this.cllId = cllId;
	}

	public int getBattery() {
		return battery;
	}

	public void setBattery(int battery) {
		this.battery = battery;
	}

	public String getSendTime() {
		return sendTime;
	}

	public void setSendTime(String sendTime) {
		this.sendTime = sendTime;
	}

	public int getSerial() {
		return serial;
	}

	public void setSerial(int serial) {
		this.serial = serial;
	}

	public int getSend() {
		return send;
	}

	public void setSend(int send) {
		this.send = send;
	}
}